package com.qlogic.commons.platform.impls.ftp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;

import com.qlogic.commons.exceptions.BaseException;
import com.qlogic.commons.platform.AbstractService;
import com.qlogic.commons.platform.ServiceActivationException;
import com.qlogic.commons.platform.ServiceDesactivationException;
import com.qlogic.commons.platform.ServiceExecutionException;
import com.qlogic.commons.platform.ServiceInitializationException;
import com.qlogic.commons.platform.ServiceStartingException;
import com.qlogic.commons.platform.ServiceStopingException;

/**
 * @version $Date: 2005/08/05 19:00:53 $, $Revision: 1.2 $, branche-$Name:  $  
 * @author $Author: azi $ 
 */
public class FTPService extends AbstractService {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 779426402476524298L;
	protected static final String HOST = "ftp.host";
	protected static final String PORT = "ftp.port";
	protected static final String USER = "ftp.user";
	protected static final String PSWD = "ftp.password";
	protected static final String TIMEOUT = "ftp.timeout";
	
	protected FTPClient client;
	protected String host;
	protected int port;
	protected int timeout;
	
	public FTPService () {
	}

	/**
	 * @see com.qlogic.commons.platform.Service#initialize()
	 */
	public void initialize () throws ServiceInitializationException {
		try {
			host = (String)configuration.getResource (HOST).getValue ();
			port = ((Integer)configuration.getResource (PORT).getValue ()).intValue();
			timeout = ((Integer)configuration.getResource (TIMEOUT).getValue ()).intValue();
		} catch (BaseException bex) {
			throw new ServiceInitializationException (bex);
		}
	}

	/**
	 * @see com.qlogic.commons.platform.Service#activate()
	 */
	public void activate () throws ServiceActivationException {
		activated = true;
	}

	/**
	 * @see com.qlogic.commons.platform.Service#start()
	 */
	public void start () throws ServiceStartingException {
		started = true;
	}

	/**
	 * @see com.qlogic.commons.platform.Service#desactivate()
	 */
	public void desactivate () throws ServiceDesactivationException {
		activated = false;
	}

	/**
	 * @see com.qlogic.commons.platform.Service#stop()
	 */
	public void stop() throws ServiceStopingException {
		started = false;
	}

	public void login (boolean active)  throws ServiceExecutionException {
		try {
			client = new FTPClient(host,port);
			if(timeout != 0) {
				client.setTimeout(timeout);
			} else {
				client.setTimeout(0);
			}
			
			if (active) {
				client.setConnectMode (FTPConnectMode.ACTIVE); 
			}
			client.login ((String)configuration.getResource (USER).getValue(), 
						  (String)configuration.getResource (PSWD).getValue());
		} catch (BaseException bex) {
			throw new ServiceExecutionException (bex);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}
	}
	
	public void login ()  throws ServiceExecutionException {
		login (false);
	}
	
	public void put (String localPath, String remoteFile) throws ServiceExecutionException {
		put (localPath, remoteFile, false);
	}
	
	private void put (String localPath, String remoteFile,  boolean append) throws ServiceExecutionException  {
		try {
			client.put (localPath, remoteFile, append);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}
	}
	
	public void append (String localPath, String remoteFile) throws ServiceExecutionException  {
		put (localPath, remoteFile, true);
	}	
	
	private void put (InputStream srcStream, String remoteFile, boolean append) throws ServiceExecutionException {
	   try {
			client.put(srcStream, remoteFile, append);
	   } catch (IOException ioe) {
		   throw new ServiceExecutionException (ioe);
	   } catch (FTPException ftpe) {
		   throw new ServiceExecutionException (ftpe);
	   }
	}
	
	public void putStream (InputStream srcStream, String remoteFile) throws ServiceExecutionException {
		put (srcStream, remoteFile, false);
	}
	
	public void appendStream (InputStream srcStream, String remoteFile) throws ServiceExecutionException {
		put (srcStream, remoteFile, true);
	}
	
	public void putBytes (byte[] bytes, String remoteFile) throws ServiceExecutionException {
		put (bytes, remoteFile, false);
	}
	
	public void appendBytes (byte[] bytes, String remoteFile) throws ServiceExecutionException {
		put (bytes, remoteFile, true);
	}
	
	public void put (byte[] bytes, String remoteFile, boolean append) throws ServiceExecutionException {
		try {
			client.put (bytes, remoteFile, append);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}
	}
	
	public void get (String localPath, String remoteFile) throws ServiceExecutionException {
		try {
			client.get (localPath, remoteFile);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}
	}
	
	public void get (OutputStream destStream, String remoteFile) throws ServiceExecutionException {
		try {
			client.get (destStream, remoteFile);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}
	}
	
	public byte[] get (String remoteFile) throws ServiceExecutionException {
		try {
			return client.get (remoteFile);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
 
	public boolean site (String command) throws ServiceExecutionException {
		try {
			return client.site (command);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	} 

	public String list (String dirname) throws ServiceExecutionException {
		return list (dirname, false);
	}
	
	public String list (String dirname, boolean full) throws ServiceExecutionException {
		try {
			return client.list (dirname, full);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public String[] dir () throws ServiceExecutionException {
		return dir(null, false);
	}
	
	public String[] dir (String dirname) throws ServiceExecutionException {
		return dir(dirname, false);
	}
	
	public String[] dir (String dirname, boolean full) throws ServiceExecutionException {
		try {
			return client.dir (dirname, full);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public void delete (String remoteFile) throws ServiceExecutionException {
		try {
			client.delete (remoteFile);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public void rename (String from, String to) throws ServiceExecutionException {
		try {
			client.rename (from, to);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public void rmdir (String dir) throws ServiceExecutionException {
		try {
			client.rmdir (dir);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public void mkdir (String dir) throws ServiceExecutionException {
		try {
			client.mkdir (dir);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public void chdir (String dir) throws ServiceExecutionException {
		try {
			client.chdir (dir);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public Date modtime (String remoteFile) throws ServiceExecutionException {
		try {
			return client.modtime (remoteFile);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public String pwd () throws ServiceExecutionException {
		try {
			return client.pwd ();
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public String system () throws ServiceExecutionException {
	   try {
		   return client.system ();
	   } catch (IOException ioe) {
		   throw new ServiceExecutionException (ioe);
	   } catch (FTPException ftpe) {
		   throw new ServiceExecutionException (ftpe);
	   }	
   }
   
	public String help (String command) throws ServiceExecutionException {
		try {
			return client.help (command);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}

	public void cd (String dir) throws ServiceExecutionException {
		try {
			client.cd (dir);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}	
	public void quit () throws ServiceExecutionException {
		try {
			if (client != null) {
				client.quit ();
			}
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public void quote (String command, String[] validCodes) throws ServiceExecutionException {
		try {
			client.quote (command, validCodes);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
		
	}
	
	public String getRemoteHostName () {
		return client.getRemoteHostName ();
	}
	
	private void setType (FTPTransferType type) throws ServiceExecutionException {
		try {
			client.setType (type);
		} catch (IOException ioe) {
			throw new ServiceExecutionException (ioe);
		} catch (FTPException ftpe) {
			throw new ServiceExecutionException (ftpe);
		}	
	}
	
	public void bin () throws ServiceExecutionException {
		setType (FTPTransferType.BINARY);
	} 

	public void ascii () throws ServiceExecutionException {
		setType (FTPTransferType.ASCII);
	} 

	private FTPTransferType getType () {
		return client.getType ();
	}

	public String type () {
		if (getType() == FTPTransferType.ASCII) {
			return "ASCII";
	    } else { 
			return "BINARY";
		}
	}

	private FTPReply getLastValidReply () {
		return client.getLastValidReply ();
	}
	
	public String lastValidReply () {
		return getLastValidReply ().getReplyCode () + "-" + getLastValidReply ().getReplyText ();
	}
	
	public void debug (boolean on) {
		client.debugResponses (on);
	}
	
	public void debugOn () {
		debug (true);
	}
	
	public void debugOff () {
		debug (false);
	}
	
	public void logStream (PrintWriter log) {
		client.setLogStream (log);
	}

}
